﻿var TerminalMgr = {
    terminalInfo: "sv.terminal.info",
    terminalTabList: "sv.terminal.tab.list",
    terminalTabCurMainTab: "sv.terminal.cur.tab.main",
    terminalTabCurSubTab: "sv.terminal.cur.tab.sub",
    terminalNearbyList: "sv.current.nearbyList",
    nearVenderCount: "sv.current.Count",
    invalidTabId: -1,
    promotionTabId: 0,
    vendingMachineTabId: 1,
    exquisiteBrandTabId: 2,
    conveningStoreTabId: 3,
    setTerminalInfo: function(terminalInfo) {
        setStorage(TerminalMgr.terminalInfo, terminalInfo);
        if (null != terminalInfo) {
            AddressMgr.setCurAddr(terminalInfo.name, terminalInfo.address, terminalInfo.gps.latitude, terminalInfo.gps.longitude);
        }
    },
    getTerminalInfo: function(){
        return getStorageVal(TerminalMgr.terminalInfo);
    },
    setCurTerminalNearbyList: function(nearbyList) {
        setStorage(TerminalMgr.terminalNearbyList, nearbyList);
    },
    getCurTerminalNearbyList: function() {
        return getStorageVal(TerminalMgr.terminalNearbyList);
    },
    getCurTerminalNearbyListData: function(showNearbyList, hideNearbyList) {
        var nearbyTerminalList = TerminalMgr.getCurTerminalNearbyList();
        if (null == nearbyTerminalList) {
            TerminalMgr.getCurTerminalNearbyListDataFromService(showNearbyList, hideNearbyList);
        } else {
            var myObj = eval(nearbyTerminalList);
            if (myObj.length > 0) {
                showNearbyList(myObj);
            } else {
                hideNearbyList();
            }
        }
    },
    getCurTerminalNearbyListDataFromService: function (showNearbyList, hideNearbyList) {
        var curTerminalInfo = TerminalMgr.getTerminalInfo();
        var historyTerminalRequestJson = {
            "type": curTerminalInfo.type,
            "gps_info": {
                "latitude": curTerminalInfo.gps.latitude,
                "longitude": curTerminalInfo.gps.longitude,
            }
        }
        var command = "nearby_point";
        doAjax(historyTerminalRequestJson, command, function (result) {
            if (result.result == 0) {
                TerminalMgr.setCurTerminalNearbyList(result.point_info);
                showNearbyList(result.point_info);
            } else {
                hideNearbyList();
            }
        });
    },
    resetCurTerminalNearbyList: function() {
        removeStorage(TerminalMgr.terminalNearbyList);
    },
    getCurTerminalId: function() {
        var curTerminalId = -1;
        var curTerminalInfo = TerminalMgr.getTerminalInfo();
        if (null != curTerminalInfo) {
            curTerminalId = Number(curTerminalInfo.id);
        }
        return curTerminalId;
    },
    setNearVenderCount: function (nearVenderCount) {
        setStorage(TerminalMgr.nearVenderCount, nearVenderCount);
    },
    getNearVenderCount: function() {
        return getStorageVal(TerminalMgr.nearVenderCount);
    },
    setTerminalTabList: function (terminalInfo, tabList, defIndex, nearVenderCount) {
        var curTerminalId = TerminalMgr.getCurTerminalId();
        if (curTerminalId != terminalInfo.id) {
            TerminalMgr.resetTabList();
            TerminalMgr.resetCurTerminalNearbyList();
            TerminalMgr.setTerminalInfo(terminalInfo);
            TerminalMgr.setNearVenderCount(nearVenderCount);
            TerminalMgr.saveTerminalTabInfo(tabList, defIndex);
        }
    },
    saveTerminalTabInfo: function (tabList, defIndex) {
        var mainTab = tabList[defIndex].type;
        var subTab = TerminalMgr.invalidTabId;
        for (mainIndex in tabList) {
            if (defIndex == mainIndex) {
                var subTabList = tabList[mainIndex].sub_menu_list;
                if (null != subTabList) {
                    subTab = subTabList[0].type;
                }
                break;
            }
        }
        setStorage(TerminalMgr.terminalTabList, tabList);
        TerminalMgr.setCurTabKey(mainTab, subTab);
    },
    setCurTabKey: function (mainTab, subTab) {
        setStorage(TerminalMgr.terminalTabCurMainTab, mainTab);
        setStorage(TerminalMgr.terminalTabCurSubTab, subTab);
    },
    getCurMainTab: function () {
        var mainTab = TerminalMgr.invalidTabId;
        var mainTabVal = getStorageVal(TerminalMgr.terminalTabCurMainTab);
        if (null != mainTabVal) {
            mainTab = Number(mainTabVal);
        }
        return mainTab;
    },
    getCurSubTabFromMainTab: function(mainTab) {
        var subTab = TerminalMgr.invalidTabId;
        var tabList = TerminalMgr.getTerminalTabList();
        if (null != tabList) {
            for (index in tabList) {
                if (tabList[index].type == mainTab) {
                    var subMenuList = tabList[index].sub_menu_list;
                    if (null != subMenuList && subMenuList.length > 0) {
                        subTab = subMenuList[0].type;
                    }
                }
            }
        }
        return subTab;
    },
    getCurSubTab: function () {
        var subTab = TerminalMgr.invalidTabId;
        var subTabVal = getStorageVal(TerminalMgr.terminalTabCurSubTab);
        if (null != subTabVal) {
            subTab = Number(subTabVal);
        }
        return subTab;
    },
    resetTabList: function () {
        var tabList = TerminalMgr.getTerminalTabList();
        if (null != tabList) {
            for (index in tabList) {
                var mainTab = tabList[index].type;
                var subTab = TerminalMgr.invalidTabId;
                var subTabList = tabList[index].sub_menu_list;
                if (null == subTabList) {
                    GoodsMgr.resetGoodsList(mainTab, subTab);
                } else {
                    for (subIndex in subTabList) {
                        subTab = subTabList[subIndex].type;
                        GoodsMgr.resetGoodsList(mainTab, subTab);
                    }
                }                
            }
        }
        removeStorage(TerminalMgr.terminalTabList);
    },
    getTerminalTabList: function () {
        return getStorageVal(TerminalMgr.terminalTabList);
    },
    getTerminalTabDefIndex: function () {
        var defIndex = 0;
        var defIndexVal = getStorageVal(TerminalMgr.terminalTabDefIndex);
        if (null != defIndexVal) {
            defIndex = Number(defIndexVal);
        }
        return defIndex;
    },
    
    getMainMenuList: function (showTerminal, showNoTerminal) {
        var gpsRequestJson = AddressMgr.getGPSRequestJson();
        doAjax(gpsRequestJson, "main_menu_list", function (response) {
            if (response.result == 0) {
                TerminalMgr.setTerminalTabList(response.vender_id, response.main_menu_list, response.default_menu, response.vender_count);
                if (showTerminal) {
                    showTerminal();
                }
            } else if (response.result == 11) {
                if (showNoTerminal) {
                    showNoTerminal();
                }
            }
        });
    },
    getFlagFallPrice: function () {
        var flagFallPrice = 30;
        var terminalInfo = TerminalMgr.getTerminalInfo();
        if (null != terminalInfo) {
            flagFallPrice = Number(terminalInfo.flag_fall_price);
        }
        return flagFallPrice;
    },
};